package com.elock.intf.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.entity.CompanyInfo;
import com.elock.entity.SaleOrg;
import com.elock.entity.Terminal;
import com.elock.entity.TerminalOnline;
import com.elock.entity.TerminalRealtimePosition;
import com.elock.entity.TerminalSaleOrg;
import com.elock.intf.dao.IntfSaleOrgDao;
import com.elock.intf.dao.IntfTerminalDao;
import com.elock.intf.service.IntfTerminalService;
import com.elock.service.CompanyInfoService;
import com.elock.service.SaleOrgBaseService;
import com.elock.service.TerminalOnlineService;
import com.elock.service.TerminalRealtimePositionService;
import com.elock.service.TerminalSaleOrgService;

/**
 * 终端设备
 * @author 仁德
 */
@Service("intfTerminalServiceImpl")
public class IntfTerminalServiceImpl extends BaseServiceImpl<Terminal>
		implements IntfTerminalService {

	@Resource(name = "terminalSaleOrgServiceImpl")
	private TerminalSaleOrgService terminalSaleOrgService;
	@Resource(name = "terminalOnlineServiceImpl")
	private TerminalOnlineService terminalOnlineService;
	@Resource(name = "terminalRealtimePositionServiceImpl")
	private TerminalRealtimePositionService terminalRealtimePositionService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;
	@Resource(name = "intfTerminalDao")
	private IntfTerminalDao intfTerminalDao;
	@Resource(name = "intfSaleOrgDao")
	private IntfSaleOrgDao intfSaleOrgDao;

	@Override
	@Transactional
	public void update(String companyCode, Terminal terminal, String saleOrgSn) {

		if (ConvertUtil.isEmpty(companyCode)) {
			ExceptionUtil.throwServiceException("请传入企业编号");
		}
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("company_code", companyCode));
		CompanyInfo companyInfo = companyInfoService.find(filters);
		if (companyInfo == null) {
			ExceptionUtil.throwServiceException("不存在企业编号为[" + companyCode + "]的企业");
		}
		String terminalNum = terminal.getTerminalNum();
		if (ConvertUtil.isEmpty(terminalNum)) {
			ExceptionUtil.throwServiceException("请传入终端设备编号");
		}

		Terminal pTerminal = intfTerminalDao.findByTerminalNum(terminalNum);
		if (pTerminal == null) {

			terminal.setCompanyInfoId(companyInfo.getId());
			this.save(terminal);

			//终端是否在线
			TerminalOnline terminalOnline = new TerminalOnline();
			terminalOnline.setTerminalId(terminal.getId());
			terminalOnline.setTerminalNum(terminalNum);
			terminalOnline.setCompanyInfoId(companyInfo.getId());
			terminalOnlineService.save(terminalOnline);

			//终端实时位置信息
			TerminalRealtimePosition terminalRealtimePosition = new TerminalRealtimePosition();
			terminalRealtimePosition.setTerminalId(terminal.getId());
			terminalRealtimePosition.setTerminalNum(terminalNum);
			terminalRealtimePosition.setCompanyInfoId(companyInfo.getId());
			terminalRealtimePositionService.save(terminalRealtimePosition);
		}
		else {
			if (!ConvertUtil.isEmpty(terminal.getTerminalName())) {
				pTerminal.setTerminalName(terminal.getTerminalName());
			}
			if (!ConvertUtil.isEmpty(terminal.getSim())) {
				pTerminal.setSim(terminal.getSim());
			}
			if (!ConvertUtil.isEmpty(terminal.getRemark())) {
				pTerminal.setRemark(terminal.getRemark());
			}
			if (terminal.getIsManualRemoveWarn() != null) {
				pTerminal.setIsManualRemoveWarn(terminal.getIsManualRemoveWarn());
			}
			if (terminal.getIsManualUnlock() != null) {
				pTerminal.setIsManualUnlock(terminal.getIsManualUnlock());
			}
			this.update(pTerminal);
		}

		//设置终端设备机构关联
		SaleOrg saleOrg = intfSaleOrgDao.findBySaleOrgSn(saleOrgSn);
		saveTerminalSaleOrg(saleOrg == null ? null : saleOrg.getId(), pTerminal);
	}

	/**
	 * 保存终端设备机构关联
	 */
	public void saveTerminalSaleOrg(Long saleOrgId, Terminal terminal) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("terminalId", terminal.getId()));
		List<TerminalSaleOrg> terminalSaleOrgs = terminalSaleOrgService.findList(null,
				filters,
				null);
		TerminalSaleOrg terminalSaleOrg = null;
		for (TerminalSaleOrg org : terminalSaleOrgs) {
			if (saleOrgId != null && org.getSaleOrgId().equals(saleOrgId)) {
				terminalSaleOrg = org;
			}
			else {
				terminalSaleOrgService.delete(org);
			}
		}
		if (terminalSaleOrg == null && saleOrgId != null) {
			terminalSaleOrg = new TerminalSaleOrg();
			terminalSaleOrg.setTerminalId(terminal.getId());
			terminalSaleOrg.setTerminalNum(terminal.getTerminalNum());
			terminalSaleOrg.setSaleOrgId(saleOrgId);
			terminalSaleOrgService.save(terminalSaleOrg);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Map<String, Object>> findPage(Pageable pageable, Object[] args) {
		return intfTerminalDao.findPage(pageable, args);
	}
}
